package pers.cz.database;

import entity.DataBaseParamter;
import pers.cz.log.LogUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @program: LuoThinking-cloud
 * @description: 抽象数据库相关操作类
 * @author: Cheng Zhi
 * @create: 2022-08-08 10:59
 **/
public abstract class DataBaseOperator implements DBoperator {

    Connection connection;

    Statement statement;

    public DataBaseOperator(DataBaseParamter dataBaseParamter) {

        String dbType = dataBaseParamter.getType();
        String driver = "";
        String url = "";
        String ip = dataBaseParamter.getIp();
        String port = dataBaseParamter.getPort();
        String dbName = dataBaseParamter.getDataBaseName();
        String userName = dataBaseParamter.getUserName();
        String passWord = dataBaseParamter.getPassword();
        url = "jdbc" + ":" + dbType + ":@" + ip + ":" + port + ":"
                + dbName;
        if (dbType.equalsIgnoreCase("mysql")) {
            driver = "com.mysql.jdbc.Driver";
        } else if (dbType.equalsIgnoreCase("oracle")) {
            driver = "oracle.jdbc.driver.OracleDriver";
        } else if (dbType.equalsIgnoreCase("mariadb")) {
            driver = "org.mariadb.jdbc.Driver";
        }
        getSatement(driver, url, userName, passWord);

    }

    /**
     * 执行sql
     */
    public void execSql(String sql) {
        try {
            execSql0(sql);
        } finally {
            try {
                connection.close();
                statement.close();
            } catch (SQLException e) {
                LogUtils.error("连接关闭异常", e);
            }

        }
    }

    public abstract void execSql0(String sql);

    /**
     * 获取statement对象
     * @param driver
     * @param url
     * @param userName
     * @param passWord
     * @return
     */
    public Statement getSatement(String driver, String url, String userName, String passWord) {

        try {
            // 通过反射获取驱动
            Object o = Class.forName(driver).newInstance();
            // 保证单例
            if (connection == null) {
                connection = DriverManager.getConnection(url, userName, passWord);
            }

            if (statement == null) {
                statement = connection.createStatement();
            }
            return statement;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("连接数据库出错");
        }

        return statement;
    }

    /**
     * 获取Connection对象
     * @param driver
     * @param url
     * @param userName
     * @param passWord
     * @return
     */
    public Connection  getConnection(String driver, String url, String userName, String passWord) {

        try {
            // 通过反射获取驱动
            Object o = Class.forName(driver).newInstance();
            // 保证单例
            if (connection == null) {
                connection = DriverManager.getConnection(url, userName, passWord);
            }
            return connection;
        } catch (Exception e) {
            System.out.println("连接数据库出错");
        }

        return connection;
    }
}
